import db from '../../config/sqlcfg';

class EthBlock {
  
  private static table: string = 'eth_block';

  /**
   * 保存区块扫描记录
   * @param blockNum 区块高度
   * @param transactionNum 交易数量
   */
  static async saveBlockLog(blockNum: number, transactionNum: number) {
    let ther = await db.query(`select * from ${this.table} where block_num = ${blockNum}`);
    let save: object;
    if(ther.length) { 
      save = await db.query(`update ${this.table} set affert_num = affert_num + 1 where block_num = ${blockNum}`);
    }else{
      save = await db.query(`insert into ${this.table} set block_num = ${blockNum}, transaction_num = ${transactionNum}`);
    }
    return save;
  }

  /**
   * 捕获需求交易数量添加
   * @param blockNum 区块高度
   */
  static async captureNumAdd(blockNum: number) {
    let update: object = await db.query(`update ${this.table} set capture_num = capture_num + 1 where block_num = ${blockNum}`);
    return update;
  }

  /**
   * 获取扫描过的最后一个区块
   */
  static async getLastBlockNum() {
    let rows = await db.query(`select block_num, affert_num from ${this.table} order by block_num desc limit 1`);
    if(rows.length){
      return rows[0];
    }else{
      return false;
    }
    
  }
}

export default EthBlock;